package com.jy.adscm.wechat.service.impl;

import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.wechat.bean.*;
import com.jy.adscm.wechat.mapper.mysql.JqbzSettingsMapper;
import com.jy.adscm.wechat.mapper.mysql.ShCustomerInfoMapper;
import com.jy.adscm.wechat.mapper.mysql.WechatPartsMapper;
import com.jy.adscm.wechat.mapper.mysql.WechatUserMapper;
import com.jy.adscm.wechat.service.WechatPartsService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.wechat.service.WechatUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-09-29
 */
@Service
public class WechatPartsServiceImpl extends ServiceImpl<WechatPartsMapper, WechatParts> implements WechatPartsService {
    @Autowired
    ShCustomerInfoMapper shCustomerInfoMapper;
    @Autowired
    WechatUserRoleService wechatUserRoleService;
    @Autowired
    WechatUserMapper wechatUserMapper;
    @Autowired
    JqbzSettingsMapper jqbzSettingsMapper;
    @Override
    public Page<WechatParts> selectWechatParts(Page<WechatParts> page, QueryWechatParts queryWechatParts) throws AdscmSystemException {
        Boolean jzUser = wechatUserRoleService.isJzUser(queryWechatParts.getOpenid());
        WechatUser wechatUser = wechatUserMapper.selectByOpenId(queryWechatParts.getOpenid());
        if (!AdscmBaseUtils.getInstance().isNotEmpty(wechatUser,null)){
            return page;
        }
        Wrapper<WechatParts> wrapper =new EntityWrapper<>();
        if (jzUser){
            wrapper.eq("pid",wechatUser.getPlasmaStationId());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryWechatParts.getIsSign(),null)){
            wrapper.eq("is_sign",queryWechatParts.getIsSign());
        }
        page=super.selectPage(page,wrapper);
        List<WechatParts> wechatPartss = page.getRecords();
        for (WechatParts wechatParts : wechatPartss) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(wechatParts.getName(),null)){
                JqbzSettings jqbzSettings = jqbzSettingsMapper.selectById(wechatParts.getName());
                wechatParts.setJqbzSettings(jqbzSettings);
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(wechatParts.getPid(),null)){
                ShCustomerInfo shCustomerInfo = shCustomerInfoMapper.selectById(wechatParts.getPid());
                wechatParts.setShCustomerInfo(shCustomerInfo);
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(wechatParts.getCreator(),null)){
                WechatUser wechatUser1 = wechatUserMapper.selectById(wechatParts.getCreator());
                wechatParts.setCreatorName(wechatUser1.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(wechatParts.getSigner(),null)){
                WechatUser wechatUser1 = wechatUserMapper.selectById(wechatParts.getSigner());
                wechatParts.setSignerName(wechatUser1.getName());
            }
        }
        page.setTotal(selectCount(wrapper));

        return page;
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public WechatParts updateWechatPartsVaule(Long pid, Float nums) throws AdscmSystemException{
        synchronized (this){
            WechatParts wechatParts = super.selectById(pid);
            if (nums>0){
                if (wechatParts.getRemain()==0){
                    throw  new AdscmSystemException("该部件已使用");
                }
                wechatParts.setRemain(0f);
                wechatParts.setIsOut(1);
            }else {
                wechatParts.setRemain(1f);
                wechatParts.setIsOut(0);
            }
            super.updateById(wechatParts);
            return wechatParts;
        }

    }

    @Override
    public WechatParts selectWechatPartsById(Long id) {
        WechatParts wechatParts = super.selectById(id);
        if (AdscmBaseUtils.getInstance().isNotEmpty(wechatParts.getName(),null)){
            JqbzSettings jqbzSettings = jqbzSettingsMapper.selectById(wechatParts.getName());
            wechatParts.setJqbzSettings(jqbzSettings);
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(wechatParts.getPid(),null)){
            ShCustomerInfo shCustomerInfo = shCustomerInfoMapper.selectById(wechatParts.getPid());
            wechatParts.setShCustomerInfo(shCustomerInfo);
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(wechatParts.getCreator(),null)){
            WechatUser wechatUser1 = wechatUserMapper.selectById(wechatParts.getCreator());
            wechatParts.setCreatorName(wechatUser1.getName());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(wechatParts.getSigner(),null)){
            WechatUser wechatUser1 = wechatUserMapper.selectById(wechatParts.getSigner());
            wechatParts.setSignerName(wechatUser1.getName());
        }
        return wechatParts;
    }

    @Override
    public Boolean updateWechatParts(WechatParts wechatParts, String openid)throws AdscmSystemException {
        WechatUser wechatUser = wechatUserMapper.selectByOpenId(openid);
        WechatParts oldWechatParts = super.selectById(wechatParts.getId());
        if (wechatParts.getIsSign()==1){
            if (oldWechatParts.getIsSign()==0){
                wechatParts.setSigner(wechatUser.getId().toString());
                wechatParts.setAutNums(1f);
                wechatParts.setRemain(wechatParts.getAutNums());
                wechatParts.setSignTime(new Date());
            }
        }
        boolean b = super.updateById(wechatParts);
        return b;
    }
}
